<?php

class SenioritiesController extends AppController
{

	public $name = 'Seniorities';
	public $helpers = array();
	public $uses = array('Seniority');

	public $modelName = '';
	public $po_domain = '';
	public $url_part  = '';

	function beforeFilter()
	{
		parent::beforeFilter();

		$this->modelName = Inflector::singularize($this->name);
		$this->po_domain = Inflector::underscore($this->name);
		$this->url_part  = 'lists/'.$this->po_domain;
		$this->set('po_domain', $this->po_domain);
		$this->set('url_part',  $this->url_part);
		$this->set('modelName', $this->modelName);
		$this->set('PKName',    $this->{$this->modelName}->primaryKey);
		Auth::Access('admin & active');
	}

	function create()
	{
	    $modelName = $this->modelName;
		$PKName    = $this->{$modelName}->primaryKey;
		$po_domain = $this->po_domain;
		$url_part  = $this->url_part;

		if (!empty($this->data[$modelName]['submit_back'])) {
            $this->redirect("/{$url_part}/", null, true);
            exit();
	    }
		$this->set('mode', __FUNCTION__);

	    $this->pageTitle = strip_tags(__d($po_domain, 'create_title', true));

	    $submit = !empty($this->data);
		if (! $this->{$modelName}->allowUpdate()) {
		    $this->data = array($modelName => array());
		}
		if ($this->{$modelName}->allowUpdate() && $submit) {
			$error_msg = '';
			$success_msg = __d($po_domain, 'created', true);
			$id = null;
            if (empty($error_msg)) {
				if ($this->{$modelName}->save($this->{$modelName}->create($this->data))) {
       			    $id = $this->{$modelName}->getInsertID();
       			    $this->data[$modelName][$PKName] = $id;
				} elseif (!empty($this->{$modelName}->validationErrors)) {
					$error_msg = __d(null, 'Data are incorrectly entered.', true).' '.__d(null, 'Try again.', true);
				} else {
	    		    $error_msg = __d(null, 'There was an internal error.', true).' '.__d(null, 'Try later.', true);
				}
            }
			if (empty($error_msg)) {
			    $this->msgNotice($success_msg);
			    $this->redirect("/{$url_part}/{$id}/", null, true);
			    exit();
			} else {
			    $this->msgError($error_msg, @$error_code);
			}
		}

		$this->data[$modelName]['__allows']['update'] = $this->{$modelName}->allowUpdate();
		$this->data[$modelName]['__allows']['delete'] = $this->{$modelName}->allowDelete();

		$this->render('details');
	}

	function list_of()
	{
		vendor('date');

	    $modelName = $this->modelName;
		$PKName    = $this->{$modelName}->primaryKey;
		$po_domain = $this->po_domain;
		$url_part  = $this->url_part;

		$this->pageTitle = strip_tags(__d($po_domain, 'list_title', true));

		vendor('contingencies');
		vendor('order_html');
		vendor('pagination_html');
		vendor('page_of_data');

		$contingencies = new Contingencies();
        $contingencies->model =& $this->{$modelName};
        $contingencies->expects = '';

        $appearence = new OrderHTML();
        $appearence->orderFields = array(
            'id' => "`{$modelName}`.`{$PKName}`",
            'name' => "`{$modelName}`.`name`",
        );
        $appearence->defaultOrder = Order::parseString('id');
        $appearence->processParams();

        $pagination = new PaginationHTML();
        $pagination->ns = __CLASS__.':'.__FUNCTION__;
        $pagination->defaultLimit = 20;
        $pagination->processParams();

        $list = new PageOfData($contingencies, $appearence, $pagination);

        $count = count($list->rows);
        for ($i = 0; $i < $count; $i++) {
            $row =& $list->rows[$i][$modelName];
            $row['__allows'] = array(
                'update' => $this->{$modelName}->allowUpdate($row),
                'delete' => $this->{$modelName}->allowDelete($row),
            );
        }

		$this->set('list', $list);
	}

	function details($id = null)
	{
	    $modelName = $this->modelName;
		$PKName    = $this->{$modelName}->primaryKey;
		$po_domain = $this->po_domain;
		$url_part  = $this->url_part;

		if (!empty($this->data[$modelName]['submit_back'])) {
            $this->redirect("/{$url_part}/", null, true);
            exit();
	    }
		$this->set('mode', __FUNCTION__);
		$this->pageTitle = strip_tags(__d($po_domain, 'details_title', true));

	    if (isset($this->params['id'])) {
	        $id = $this->params['id'];
	    }

	    $this->{$modelName}->rel();
        $tmp = $this->{$modelName}->read(null, $id);

		if (! is_array($tmp) || !count($tmp)) {
			$this->msgError(__d($po_domain, 'absent', true), @$error_code);
			$this->redirect("/{$url_part}/", null, true);
			exit();
		}

		////// MENU ///////////////////////

	    $ancestors_menu =& $this->MainMenu->findItem("/{$url_part}/");
	    $this->MainMenu->setActive($ancestors_menu);

		///////////////////////////////////

		$submit = !empty($this->data);
		if (! $this->{$modelName}->allowUpdate($tmp[$modelName])) {
		    $this->data = array($modelName => array());
		}
		foreach ($tmp as $_modelName => $_modelData) {
            if ($_modelName !== $modelName) {
    			$this->data[$_modelName] = $tmp[$_modelName];
            } else {
        		$this->data[$modelName] = am($tmp[$modelName], $this->data[$modelName]);
            }
		}

		if ($this->{$modelName}->allowUpdate($tmp[$modelName]) && $submit) {
			$error_msg = '';
			$success_msg = __d($po_domain, 'changed', true);
            if (empty($error_msg)) {
				if ($this->{$modelName}->save($this->data)) {

				} elseif (!empty($this->{$modelName}->validationErrors)) {
					$error_msg = __d(null, 'Data are incorrectly entered.', true).' '.__d(null, 'Try again.', true);
				} else {
	    		    $error_msg = __d(null, 'There was an internal error.', true).' '.__d(null, 'Try later.', true);
				}
            }
			if (empty($error_msg)) {
			    $this->msgNotice($success_msg);
			    $this->redirect("/{$url_part}/{$id}/", null, true);
			    exit();
			} else {
			    $this->msgError($error_msg, @$error_code);
			}
		}

		$this->data[$modelName]['__allows']['update'] = $this->{$modelName}->allowUpdate($tmp[$modelName]);
		$this->data[$modelName]['__allows']['delete'] = $this->{$modelName}->allowDelete($tmp[$modelName]);
	}

	function remove($id = null)
	{
	    $modelName = $this->modelName;
		$PKName    = $this->{$modelName}->primaryKey;
		$po_domain = $this->po_domain;
		$url_part  = $this->url_part;

	    if (isset($this->params['id'])) {
	        $id = $this->params['id'];
	    }
		if (!$id) {
			$this->msgError(__d($po_domain, 'absent', true), @$error_code);
		} else {
            $this->{$modelName}->rel();
		    $data = $this->{$modelName}->read(null, $id);
		    if (is_array($data) && count($data)) {
		    	if ($this->{$modelName}->allowDelete($data[$modelName])) {
	    		    if ($this->{$modelName}->del($id)) {
	    	       		$this->msgNotice(__d($po_domain, 'deleted', true));
	        		} else {

	        		}
		    	} else {
			        $this->msgError(__d(null, 'deny', true), @$error_code);
		    	}
		    } else {
		        $this->msgError(__d($po_domain, 'absent', true), @$error_code);
		    }
		}
		$this->redirect("/{$url_part}/", null, true);
		exit();
	}

}

?>